<?php

class indexControl extends commonControl {

    //版权信息|1
    function index() {
        $_SESSION['step'] = 1;
        $this->display();
    }

    //检测环境|2
    function check() {
        if ($_SESSION['step'] < 1) {
            go("index");
        }
        $_SESSION['step'] = 2;
        $uploadsize = ini_get('upload_max_filesize');
        $gd = gd_info();
        $gdversion = $gd['GD Version']; 
        $diskspace = get_size(disk_free_space('.'));
        $system = array(
            "phpversion" => PHP_VERSION,
            "phpos" => PHP_OS,
            "uploadsize" => $uploadsize,
            "gdversion" => $gdversion,
            "diskspace" => $diskspace
        );
        $root = dirname(PATH_ROOT);
        $this->assign('system',$system);

        $this->display();
    }

    //配置数据库连接|3
    function setconfig() {

        if ($_SESSION['step'] < 2) {
            go("check");
        }
        $_SESSION['step'] = 3;
        $this->display();
    }

    //写入数据库配置
    function writeconfig() {
        $link = @mysql_connect($_POST['db']['DB_HOST'] . ":" . $_POST['db']['DB_PORT'], $_POST['db']['DB_USER'], $_POST['db']['DB_PASSWORD']);
        $select_db = @mysql_select_db($_POST['db']['DB_DATABASE']);
        if (!$link || !$select_db) {
            $this->error("数据库配置错误");
        }
        $str = "<?php \r\n";
        $str.= "if (!defined('PATH_HD'))exit;\nreturn ";
        $str.= var_export($_POST['db'], true);
        $str.= "; \r\n?>";
        $dbconfigfile = "../config/dbconfig.php";
        if(file_put_contents($dbconfigfile, $str)){
            go('installdb');
        }else{
            $this->error("config目录不可写，请修改权限");
        }

    }

    //安装数据库|4
    function installdb() {
        C(include '../config/dbconfig.php');
        backup::recovery(array(
           "dir" => './backup',
           "url" => "__success",
           "step_time"=>0,
           )
        );
    }

    function __success(){
        $root=dirname(__ROOT__);
        $this->success("安装成功","$root");
    }
}